import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.util.*; 


public class bruteforce3 {
        
public void mapReduce(String WORD_FILE,String[] RAW_FILE) throws IOException{
                
                
                PrintWriter writer = new PrintWriter("output.txt", "UTF-8");
                
                long time1 = System.currentTimeMillis();
                long time2;

                for(int num = 0;num<RAW_FILE.length;num++){
                        
                        Scanner linereader = new Scanner( new File(WORD_FILE));
                        
                        String KeyWord;
                        int Loc =0;
                        boolean matched=false;
                        
                        int line = 0;
                        RandomAccessFile file1 = new RandomAccessFile(RAW_FILE[num], "r");
                        FileChannel inChannel = file1.getChannel();
                        MappedByteBuffer longByte = inChannel.map(FileChannel.MapMode.READ_ONLY, 0, inChannel.size());
                        longByte.load();
                        while(linereader.hasNext()){
                                line++;
                                boolean first=true;
                                time2 = System.currentTimeMillis();
                                System.out.print((time2-time1) + "  ");
                                
                                KeyWord = linereader.next();
                                System.out.println(line+ " Word => "+KeyWord);
                               // writer.printf(RAW_FILE[num]+","+KeyWord);
                                byte[] keyByte = new byte[KeyWord.length()];
                                keyByte = KeyWord.getBytes();
                                //System.out.println("Loc = "+Loc+" Key = "+keyByte.length+" Str = "+longByte.length);
                                
                        for( Loc = 0;Loc<(longByte.limit()-(keyByte.length-1));Loc++){ 
                                
                                if(longByte.get(Loc)==keyByte[0]){
                                        matched = false;
                                        
                                        for(int i=1;i<keyByte.length;i++){
                                                //System.out.print(" "+i);
                                                if(longByte.get(Loc+i)==keyByte[i]){
                                                        matched = true;
                                                        
                                                }else{
                                                        matched = false;
                                                        break;
                                                }
                                        }
                                        //System.out.println();
                                        if(matched){
                                        	
                                        	if(first)	
                                        	  {writer.printf(RAW_FILE[num]+","+KeyWord);
                                               first = false;
                                               }
                                        	writer.printf(","+Loc);
                                                
                                        }
                                }
                        }
                        
                        //System.out.println("Loc = "+Loc);
                     if(!first)
                     writer.println();
                 }
                        
                        file1.close();
                        longByte.clear(); // do something with the data and clear/compact it.
                        inChannel.close();                     
                        linereader.close();
                }
                writer.close();
                
                time2 = System.currentTimeMillis();
                System.out.print((time2-time1) + "  ");
        }
        
        public static void main(String[] args) throws IOException {
                bruteforce3 boot = new bruteforce3();

                String[] raw = {"2.dat"};
                
                long time1 = System.currentTimeMillis();
                long time2;
                
                System.out.println("Running MapReduce....");
                boot.mapReduce("query.dat",raw);
                
                time2 = System.currentTimeMillis();
                
                System.out.print("Finished => ");
                System.out.print((time2-time1)/60000 + ":"+ ((time2-time1)%60000)/1000);
    }
}